package com.intsig.airflow.web.ajax;

import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.io.Serializable;

/**
 * @author leojie 2021/3/13 1:46 下午
 */
@Data
public class RestResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private boolean success;
    private int code = 200;
    private String msg;
    private long timestamp = DateUtil.currentSeconds();
    private T data;

    public RestResponseResult() {

    }

    public RestResponseResult(boolean success) {
        this.success = success;
    }

    public RestResponseResult(boolean success, T data) {
        this.success = success;
        this.data = data;
    }

    public RestResponseResult<T> success(boolean success) {
        this.success = success;
        return this;
    }

    public RestResponseResult<T> data(T data) {
        this.data = data;
        return this;
    }

    public RestResponseResult<T> code(int code) {
        this.code = code;
        return this;
    }

    public RestResponseResult<T> message(String msg) {
        this.msg = msg;
        return this;
    }

    public static <T> RestResponseResult<T> ok() {
        return (new RestResponseResult<T>()).success(true);
    }

    public static <T> RestResponseResult<T> ok(T data) {
        return (new RestResponseResult<T>()).success(true).data(data);
    }

    public static <T> RestResponseResult<T> ok(T data, int code) {
        return (new RestResponseResult<T>()).success(true).data(data).code(code);
    }

    public static <T> RestResponseResult<T> failed() {
        return (new RestResponseResult<T>()).success(false);
    }

    public static <T> RestResponseResult<T> failed(T data) {
        return (new RestResponseResult<T>()).success(false).data(data);
    }

    public static <T> RestResponseResult<T> failed(String errorMsg) {
        return (new RestResponseResult<T>()).success(false).message(errorMsg);
    }

    public static <T> RestResponseResult<T> failed(T data, int code) {
        return (new RestResponseResult<T>()).success(false).data(data).code(code);
    }
}
